package cn.yimu.util;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Random;

public abstract class CommonUtil {
	
	public final static String INIT_VALUE_OF_STRING = "";
	public final static int INIT_VALUE_OF_INTEGER = 0;
	public final static String ID_OF_TABLE_IN_DATABASE = "id";
	public final static Object INIT_VALUE_OF_OBJECT = new Object();
	public final static Object NULL_OBJECT = null;
	
	/**
	 * id's length is 19 and consisting of yyyyMMddHHmmss and 5 random number
	 * 
	 * @return
	 */
	public static String generatorId() {
		String time = TimeUtil.parseCalendarToString(Calendar.getInstance(), "yyyyMMddHHmmss");
		Random r = new Random();
		String randomNum = (r.nextLong() + "").substring(0, 5);
		
		String rlt = String.format("%s%s", time, randomNum);
		assert rlt.length() == 19;
		
		return rlt;
	}
	
	public static String decodeParameterOfGetRequestInURL(String target) {
		String rlt = "";
		try {
			rlt = new String(target.getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			assert false;
		}
		return rlt;
	}
	
	/**
	 * 
	 * @param target
	 */
	public static String encrypt(String target) {
		byte[] bytes = target.getBytes();
		float number = 0f;
		for (int i = 0; i < bytes.length; i++) {
			float tmp = bytes[i] + (i + 1) * 0.3f;
			number += tmp;
		}
		
		String rlt = String.valueOf(number);
		if(rlt.length() > 33)
			rlt = rlt.substring(0, 33);
		
		return rlt;
	}
	

}
